package logic

import (
	"context"
	"edu/models/mysql"

	"edu/edurpc/edurpc"
	"edu/edurpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectListLogic {
	return &CollectListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectListLogic) CollectList(in *edurpc.CollectListRequest) (*edurpc.CollectListResponse, error) {
	var collects []mysql.CollectList
	mysql.DB.Table("collects").
		Select("novels.title,novels.category,novels.info,novels.refer,novels.image").
		Joins("LEFT JOIN novels on novels.id = collects.novel_id").
		Where("collects.user_id = ?", in.UserId).Find(&collects)
	var collectList []*edurpc.CollectList
	for _, collect := range collects {
		list := edurpc.CollectList{
			Title:    collect.Title,
			Category: collect.Category,
			Info:     collect.Info,
			Refer:    collect.Refer,
			Image:    collect.Image,
		}
		collectList = append(collectList, &list)
	}
	return &edurpc.CollectListResponse{
		List: collectList,
	}, nil
}
